package com.example.demo.controller;

import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import com.example.demo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/list")
    public List<Student> findAll() {
        return studentService.findAll();
    }
    @DeleteMapping("/{sid}")
    public Result delete(@PathVariable String sid) {
        studentService.delete(sid);
        return Result.ok("OK");
    }
    @PostMapping("/modify")
    public Result modify(@RequestParam String sid, @RequestParam String name, @RequestParam int age, @RequestParam String gender, @RequestParam Date birthday){
        if(gender.equals("1")){
            gender = "男";
        } else {
            gender = "女";
        }
        studentService.modify(sid, name, age, gender, birthday);
        return Result.ok("OK");
    }
    @PostMapping("/add")
    public Result add(@RequestParam String sid, @RequestParam String name, @RequestParam int age, @RequestParam String gender, @RequestParam Date birthday){
        if(gender.equals("1")){
            gender = "男";
        } else {
            gender = "女";
        }
        studentService.add(sid, name, age, gender, birthday);
        return Result.ok("OK");
    }
}
